﻿using PaperMaster.Chess.Bell;
using PaperMaster.Chess.ChessModel;
using PaperMaster.Info;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PaperMaster.Chess.Enemy
{
    /// <summary>
    /// 一次敌袭的控制器，它负责控制敌人的老巢，在敌人回合时执行一些敌人操作，并结束敌人回合并发出事件
    /// </summary>
    public class EnemyController : IFightBeginCommand, IFightEndCommand
    {
        protected IChessModel _chessModel;
        protected IEnemyModel _enemyModel;
        public SingleEnemyModel model;
        public EnemyController()
        {
            Init();
        }
        public EnemyController(SingleEnemyModel model)
        {
            Init();
            this.model = model;
        }
        protected virtual void Init()
        {
            _takeAttackDecorator = new AttackDecorator();
            _chessModel = Model.ModelContainer.Get<IChessModel>();
            _enemyModel = Model.ModelContainer.Get<IEnemyModel>();
            RegisterEvents();
        }

        ~EnemyController()
        {
            UnregisterEvents();
        }
        /// <summary>
        /// 对敌人老巢造成伤害
        /// </summary>
        /// <param name="attack"></param>
        public virtual void TakeDamage(IAttack attack)
        {
            IAttack atk = _takeAttackDecorator.Decorate(attack);
            model.hp -= atk.dmg;
        }
        /// <summary>
        /// 受伤时的修饰
        /// </summary>
        protected AttackDecorator _takeAttackDecorator;
        /// <summary>
        /// 注册一个伤害修饰
        /// </summary>
        /// <param name="decorator"></param>
        public virtual void RegisterTakeDmgDecorator(AttackDelegate decorator)
        {
            _takeAttackDecorator.Register(decorator);
        }
        /// <summary>
        /// 注销一个伤害修饰
        /// </summary>
        /// <param name="decorator"></param>
        public virtual void UnregisterTakeDmgDecorator(AttackDelegate decorator)
        {
            _takeAttackDecorator.Unregister(decorator);
        }
        /// <summary>
        /// 开始敌人回合
        /// </summary>
        protected virtual void OnBeginTurn()
        {

        }

        protected virtual void RegisterEvents()
        {
            PlayerTurnEndEvent.Register(OnBeginTurn);
            PlayerTurnBeginEvent.Register(OnFinishTurn);
            FightBeginEvent.Register(OnFightBegin);
        }
        protected virtual void UnregisterEvents()
        {
            PlayerTurnEndEvent.Unregister(OnBeginTurn);
            PlayerTurnBeginEvent.Unregister(OnFinishTurn);
            FightBeginEvent.Unregister(OnFightBegin);
        }

        /// <summary>
        /// 强行结束敌人回合，该接口将敲玲
        /// </summary>
        public virtual void ForceFinishTurn()
        {
            FinishTurn();
        }

        protected virtual void FinishTurn()
        {
            BellController.EndTurn();
        }
        protected virtual void UpdateUI()
        {
            AbstractFightInfoViewController.GetController().UpdateUI("OpponentHp");
        }
        protected virtual void OnFightBegin()
        {
            UpdateUI();
        }

        /// <summary>
        /// 敌人回合结束时调用
        /// </summary>
        protected virtual void OnFinishTurn()
        {

        }

        public virtual void FightEndExecute()
        {
            
        }

        public virtual void FightBeginExecute()
        {

        }

        /// <summary>
        /// 供上层每帧调用的函数
        /// </summary>
        public virtual void FrameFun()
        {

        }
    }
}
